package com.leinao.handler;

import com.leinao.session.WebSocketSessionRegister;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.annotation.Resource;

/**
 * 自定义webSocket处理器
 *
 * @author zhong.wang
 * @date 2025/2/6
 */
public class MyWebSocketHandler extends TextWebSocketHandler {

    @Resource
    private WebSocketSessionRegister socketSessionRegister;

    /**
     * 连接建立
     * @param session
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        socketSessionRegister.addSession(session);
    }

    /**
     * 连接断开
     * @param session
     * @param closeStatus
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
        socketSessionRegister.removeSession(session);
    }


    /**
     * 消息处理
     * @param session
     * @param message
     * @throws Exception
     */
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
    }


}
